package com.novoda.runbuddy.provider;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;

import com.novoda.runbuddy.R;
import com.novoda.runbuddy.util.DBUtil;

import dalvik.annotation.TestInfo;
import dalvik.annotation.TestTarget;

public class RunProviderTest extends AndroidTestCase {

	private static final String	RUN_BUDDY_DB				= "runBuddyDB1.db";
	private SQLiteDatabase		mdb;

	private final int			RUN1_ID						= 1;
	private final int			RUN1_ROUTEID				= 1;
	private final int			RUN1_CHECKPOINTID			= 1;
	private final String		RUN1_NAME					= "jog01";
	private final int			RUN1_CHCKPOINT_NO			= 1;
	private final int			RUN1_LAP					= 1;
	private final int			RUN1_SEGMENT_TIME			= 6567765;
	private final String		RUN1_CREATED_AT				= "2008-03-10T10:10:00.000";
	private final long			RUN1_TOTALTIME				= 87678687678L;

	private static final Uri	ALL_RUNS_ON_SPECIFIC_DATE	= Uri.parse("content://com.novoda.runbuddy/runs?from=X&to=Y");
	private static final Uri	ALL_RUNS_ON_SPECIFIC_ROUTE	= Uri.parse("content://com.novoda.runbuddy/routes/1/runs");
	private static final Uri	SPECIFIC_RUN_ON_ROUTE		= Uri.parse("content://com.novoda.runbuddy/routes/1/runs/1");
	private static final Uri	ALL_RUNS					= Uri.parse("content://com.novoda.runbuddy/runs");
	private static final String	TAG							= RunProviderTest.class.getName();

	private static final Uri	ALL_SEGMENTS_FOR_RUN		= Uri.parse("content://com.novoda.runbuddy/routes/1/runs/1/segments");
	private DBUtil				dbUtil						= null;

	protected void setUp() {
		if (dbUtil == null) {
			dbUtil = new DBUtil(this.getContext(), RUN_BUDDY_DB);
		}
		dbUtil.executeDB(R.raw.db_drop);
		dbUtil.createDB();
		dbUtil.insertValuesIntoDB();
		mdb = dbUtil.getDatabase();
		if (mdb == null)
			mdb = getContext().openOrCreateDatabase(RUN_BUDDY_DB, SQLiteDatabase.OPEN_READWRITE, null);
	}

	public void testQueryingAllRuns() throws Exception {
		Cursor cur = getContext().getContentResolver().query(ALL_RUNS_ON_SPECIFIC_ROUTE, new String[] { "name" }, null, null, null);
		assertNotNull(cur);
		cur.moveToLast();
		String actual = cur.getString(0);
		assertEquals("jog01", actual);
		cur.close();
	}

	public void testGettingFirstRun() throws Exception {
		Cursor cur = getContext().getContentResolver().query(SPECIFIC_RUN_ON_ROUTE, new String[] { "name" }, null, null, null);
		assertNotNull(cur);
		cur.moveToLast();
		String actual = cur.getString(0);
		assertEquals("jog01", actual);
		cur.close();
	}

	public void testInsertingNewRun() throws Exception {
		Uri uri = getContext().getContentResolver().insert(ALL_RUNS_ON_SPECIFIC_ROUTE, null);
		assertEquals(Uri.parse("content://com.novoda.runbuddy/routes/1/runs/2"), uri);
	}

	public void testSegmentQuery() throws Exception {
		Cursor cur = getContext().getContentResolver().query(ALL_SEGMENTS_FOR_RUN, new String[] { "checkpoint" }, null, null, null);
		assertNotNull(cur);
		cur.close();
	}

	public void testGettingCheckPointAsContentURI() throws Exception {
		Cursor cur = getContext().getContentResolver().query(ALL_SEGMENTS_FOR_RUN, new String[] { "lap", "checkpoint" }, null,
				null, null);
		cur.moveToLast();
		assertEquals(2, cur.getInt(0));
		assertEquals("content://com.novoda.runbuddy/checkpoints/4", cur.getString(1));
		cur.close();
	}

	public void testInsertSegmentFailsIfNoCheckPointProvided() throws Exception {
		ContentValues values = new ContentValues();
		try {
			Uri uri = getContext().getContentResolver().insert(ALL_SEGMENTS_FOR_RUN, values);
			fail("Should have thrown a SQL exception");
		} catch (Exception e) {
			assertTrue(true);
		}
	}

	public void testInsertingSegment() throws Exception {
		ContentValues values = new ContentValues();
		values.put(RunBuddyCP.RunSegments.CHECKPOINT, "content://com.novoda.runbuddy/checkpoints/4");
		Uri uri = getContext().getContentResolver().insert(ALL_SEGMENTS_FOR_RUN, values);
		assertEquals(Uri.parse("content://com.novoda.runbuddy/routes/1/runs/1/segments/5"), uri);
	}

	// public void testRetrievingAllRunsFromContentProvider() {
	// Cursor cur =
	// getContext().getContentResolver().query(RunBuddyCP.Runs.CONTENT_URI,
	// null, null, null, null);
	// assertNotNull(cur);
	// cur.moveToFirst();
	// assertEquals(RUN1_ID, cur.getInt(0));
	// assertEquals(RUN1_CHECKPOINTID, cur.getInt(2));
	// assertEquals(RUN1_NAME, cur.getString(3));
	// assertEquals(RUN1_CHCKPOINT_NO, cur.getInt(4));
	// assertEquals(RUN1_LAP, cur.getInt(5));
	// assertEquals(RUN1_SEGMENT_TIME, cur.getInt(6));
	// assertEquals(RUN1_CREATED_AT, cur.getString(7));
	// assertEquals(RUN1_TOTALTIME, cur.getLong(8));
	// cur.close();
	// }
	//
	// public void testRetrievingAllRunsBetween_2008_09_01AND_2008_10_06() {
	// SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
	// Cursor cur = null;
	//		
	// String fromParam = "from=2008-09-01T00:00:00.000";
	// String toParam = "to=2009-10-06T23:00:00.000";
	//		
	// String query = RunBuddyCP.Runs.CONTENT_URI.toString() + '?' + fromParam +
	// '&' + toParam;
	//			
	//		
	// cur = getContext().getContentResolver().query(Uri.parse(query), null,
	// null, null, null);
	//		
	// assertNotNull(cur);
	//		
	// cur.moveToFirst();
	// assertEquals(RUN1_ID, cur.getInt(0));
	// assertEquals(RUN1_ROUTEID, cur.getInt(1));
	// assertEquals(RUN1_CHECKPOINTID, cur.getInt(2));
	// assertEquals(RUN1_NAME, cur.getString(3));
	// assertEquals(RUN1_CHCKPOINT_NO, cur.getInt(4));
	// assertEquals(RUN1_LAP, cur.getInt(5));
	// assertEquals(RUN1_SEGMENT_TIME, cur.getInt(6));
	// assertEquals(RUN1_CREATED_AT, cur.getString(7));
	// assertEquals(RUN1_TOTALTIME, cur.getLong(8));
	//
	// cur.moveToLast();
	//
	// cur.close();
	// }
}
